package top.infopub.mgr.ic.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.EffectExamAttDao;
import top.infopub.mgr.ic.dao.EffectExamDao;
import top.infopub.mgr.ic.domain.EffectExamDomain;
import top.infopub.mgr.ic.service.EffectExamService;
import top.infopub.mgr.ic.vo.EffectExamVo;
import top.infopub.model.ic.EffectExam;
import top.infopub.model.ic.EffectExamAtt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class EffectExamServiceImpl extends GenericServiceImpl<EffectExam, String> implements
		EffectExamService {

	@Autowired
	private EffectExamDao effectExamDao;
	
	@Autowired
    private EffectExamAttDao effectExamAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<EffectExam, String> getDao() {
		return effectExamDao;
	}
	
	@Override
    public Page<EffectExamDomain> selectByDomainWhere(Page<EffectExamDomain> page,
                                                      EffectExamVo effectExamVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<EffectExamDomain>)effectExamDao.selectByDomainWhere(effectExamVo);
    }

    @Override
    public Page<EffectExamDomain> selectMyCheckByDomainWhere(Page<EffectExamDomain> page,
                                                             EffectExamVo effectExamVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<EffectExamDomain>)effectExamDao.selectMyCheckByDomainWhere(effectExamVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEffectExam(EffectExam effectExam, List<EffectExamAtt> effectExamAtts) {
        
        effectExamDao.insertById(effectExam);
        
        if(CollectionUtils.isNotEmpty(effectExamAtts)){
            effectExamAttDao.insertByBatch(effectExamAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.IC_EFFECT_EXAM;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, effectExam.getId(), effectExam.getProcessId(), null);
        
    }

    @Override
    public EffectExamVo queryById(String id) {
        return effectExamDao.selectById(id);
    }

}